/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "channel.h"

#include <gtest/gtest.h>
#include <memory>
#include "loghelper.h"
#include "ipc_object_stub.h"
#include "session.h"
#include "se_service.h"
#include "service_manager.h"
#include "reader.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace OHOS::SE::OMAPI;
static const std::string CHANNEL_NUMBER = "A000000151000000";
static const std::string GET_CPLC = "80CA9F7F00";
static bool g_isSeSupported = false;

class LogicalChannelTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
    void SetUp();
    void TearDown();
public:
    std::shared_ptr<SEService> seService_;
    std::shared_ptr<Reader> reader_;
    std::shared_ptr<Session> session_;
    std::shared_ptr<Channel> channel_;
};

void LogicalChannelTest::SetUp()
{
    InfoLog("LogicalChannelTest::SetUp begin!");
    seService_ = std::make_shared<SEService>();
    bool initStatus = false;
    seService_->InitSEService(initStatus, nullptr);
    sleep(1);
    EXPECT_TRUE(initStatus);
    vector<std::shared_ptr<Reader>> readerLists;
    seService_->GetReaders(readerLists);
    g_isSeSupported = readerLists.empty();
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    for (auto reader : readerLists) {
        std::string readerName {};
        reader->GetName(readerName);
        if (readerName.compare("eSE") == 0) {
            reader_ = reader;
            break;
        }
    }
    EXPECT_TRUE(reader_);
    if (reader_ != nullptr) {
        reader_->OpenSession(session_);
    }
    EXPECT_TRUE(session_);
    if (session_ != nullptr) {
        std::string response = "";
        session_->OpenLogicalChannel(CHANNEL_NUMBER, 0, channel_, response);
    }

    EXPECT_TRUE(channel_);
    InfoLog("LogicalChannelTest::SetUp done!");
}

void LogicalChannelTest::TearDown()
{
    InfoLog("LogicalChannelTest::TearDown begin!");
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_) {
        channel_->Close();
        channel_ = nullptr;
    }
    if (session_) {
        session_->Close();
        session_ = nullptr;
    }
    if (reader_) {
        reader_->CloseSessions();
        reader_ = nullptr;
    }
    if (seService_) {
        seService_->Shutdown();
        seService_ = nullptr;
    }
    InfoLog("LogicalChannelTest::TearDown done!");
}

/**
 * @tc.name: GetSession_Test001
 * @tc.desc: Test SeService GetSession.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, GetSession_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        std::weak_ptr<Session> session;
        int statusCode = channel_->GetSession(session);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(session.expired());
    }
}

/**
 * @tc.name: IsClosed_Test001
 * @tc.desc: Test SeService IsClosed.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, IsClosed_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        bool isClosed = false;
        int statusCode = channel_->IsClosed(isClosed);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(isClosed);
    }
}

/**
 * @tc.name: IsBasicChannel_Test001
 * @tc.desc: Test SeService IsBasicChannel.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, IsBasicChannel_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        bool isBasicChannel = false;
        int statusCode = channel_->IsBasicChannel(isBasicChannel);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(isBasicChannel);
    }
}

/**
 * @tc.name: GetSelectResponse_Test001
 * @tc.desc: Test SeService GetSelectResponse.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, GetSelectResponse_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        std::string response;
        int statusCode = channel_->GetSelectResponse(response);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
    }
}

/**
 * @tc.name: Transmit_Test001
 * @tc.desc: Test SeService Transmit.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, Transmit_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        std::string response;
        int statusCode = channel_->Transmit(GET_CPLC, response);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(response.length() > 0);
    }
}


/**
 * @tc.name: Close_Test001
 * @tc.desc: Test SeService Close.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, Close_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        int statusCode = channel_->Close();
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);

        bool isClosed = false;
        statusCode = channel_->IsClosed(isClosed);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(isClosed);
    }
}
/**
 * @tc.name: InnerCheckChannelStatus_Test001
 * @tc.desc: Test SeService Close.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, InnerCheckChannelStatus_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::shared_ptr<Session> session = nullptr;
    sptr<ISecureElementChannel> seChannel = nullptr;
    std::shared_ptr<Channel> channel = std::make_shared<Channel>(service, session, seChannel);
    if (channel != nullptr) {
        int statusCode = channel->Close();
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: IsBasicChannel_Test002
 * @tc.desc: Test SeService IsBasicChannel.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, IsBasicChannel_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::shared_ptr<Session> session = nullptr;
    sptr<ISecureElementChannel> seChannel = nullptr;
    std::shared_ptr<Channel> channel = std::make_shared<Channel>(service, session, seChannel);
    if (channel != nullptr) {
        bool isBasicChannel = false;
        int statusCode = channel->IsBasicChannel(isBasicChannel);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: GetSelectResponse_Test002
 * @tc.desc: Test SeService GetSelectResponse.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, GetSelectResponse_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::shared_ptr<Session> session = nullptr;
    sptr<ISecureElementChannel> seChannel = nullptr;
    std::shared_ptr<Channel> channel = std::make_shared<Channel>(service, session, seChannel);
    if (channel != nullptr) {
        std::string response;
        int statusCode = channel->GetSelectResponse(response);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: Transmit_Test002
 * @tc.desc: Test SeService Transmit.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, Transmit_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::shared_ptr<Session> session = nullptr;
    sptr<ISecureElementChannel> seChannel = nullptr;
    std::shared_ptr<Channel> channel = std::make_shared<Channel>(service, session, seChannel);
    if (channel != nullptr) {
        std::string response;
        const std::string command;
        int statusCode = channel->Transmit(command, response);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
/**
 * @tc.name: SelectNext_Test001
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, SelectNext_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_ != nullptr) {
        bool selectStatus = false;
        int statusCode = channel_->SelectNext(selectStatus);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(selectStatus);
    }
}
/**
 * @tc.name: SelectNext_Test002
 * @tc.desc: Test Reader OpenSession.
 * @tc.type: FUNC
 */
HWTEST_F(LogicalChannelTest, SelectNext_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    std::shared_ptr<Session> session = nullptr;
    sptr<ISecureElementChannel> seChannel = nullptr;
    std::shared_ptr<Channel> channel = std::make_shared<Channel>(service, session, seChannel);
    if (channel != nullptr) {
        bool selectStatus = false;
        int statusCode = channel->SelectNext(selectStatus);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}
} // TEST
} // SE
} // OHOS